using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using Entity;

public partial class admin_settings : SiteAdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;
        if (!Page.IsPostBack)
        {
            BindData();
        }

    }

    protected void Product_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.Header)
        //{
        //    CheckBox cbxSelectAll = (CheckBox)e.Row.FindControl("cbxSelectAll");
        //    cbxSelectAll.Attributes.Add("onclick", "javascript:SelectAll('" + grvProduct.ClientID + "','" + cbxSelectAll.ClientID + "','cbxSelect')");
        //}
    }

    protected void BindData()
    {
        //imbRemove.Attributes.Add("onclick", "javascript:return confirm('Are you sure that you want to delete these items?')");

        uccCompanyState.DataBind();
        uccCompanyCountry.DataBind();

        tbxCompanyName.Text = SiteSettings.CompanyName;
        tbxCompanyAddress.Text = SiteSettings.CompanyAddress;
        tbxCompanyZip.Text = SiteSettings.CompanyZip;
        tbxCompanyCity.Text = SiteSettings.CompanyCity;
        ListItem CompanyState = uccCompanyState.Items.FindByText(SiteSettings.CompanyState);
        if (CompanyState != null)
            CompanyState.Selected = true;

        ListItem CompanyCountry = uccCompanyCountry.Items.FindByText(SiteSettings.CompanyCountry);
        if (CompanyCountry != null)
            CompanyCountry.Selected = true;

        tbxCompanyPhone.Text = SiteSettings.CompanyPhone;
        tbxCompanyFax.Text = SiteSettings.CompanyFax;
        tbxCustomerPrefix.Text = SiteSettings.CustomerPrefix;
        tbxOderPrefix.Text = SiteSettings.OrderPrefix;
        tbxAdminEmail.Text = SiteSettings.AdminEmail;
        tbxStockAlertLevel.Text = SiteSettings.StockAlertLevel.ToString();
        tbxMinimumOrderAmount.Text = SiteSettings.MinimumOrderAmount.ToString();
        tbxFreeGroundShipping.Text = SiteSettings.GroundShippingAmount.ToString();
        chkIsfreeGround.Checked = SiteSettings.IsGroundShippingFree;

        //LoadProductDropDownList();
        //BindFavoriteGrid();
    }

    //private void BindFavoriteGrid()
    //{
    //    const int MAX = 99999;
    //    grvProduct.DataSource = Product.FeaturedProducts(MAX);
    //    grvProduct.DataBind();
    //}

    //private void LoadProductDropDownList()
    //{
    //    bool includeDeleted = false;
    //    ddlProductList.DataSource = Product.GetProductListExcludedFavorites(includeDeleted);
    //    ddlProductList.DataTextField = "ProductName";
    //    ddlProductList.DataValueField = "ProductId";
    //    ddlProductList.DataBind();
    //}

    protected void Create_Click(object sender, EventArgs e)
    {
        try
        {
            SiteSettings.CompanyName = tbxCompanyName.Text;
            SiteSettings.CompanyAddress = tbxCompanyAddress.Text;
            SiteSettings.CompanyZip = tbxCompanyZip.Text;
            SiteSettings.CompanyCity = tbxCompanyCity.Text;
            SiteSettings.CompanyState = uccCompanyState.SelectedItem.Text;
            SiteSettings.CompanyCountry = uccCompanyCountry.SelectedItem.Text;
            SiteSettings.CompanyPhone = tbxCompanyPhone.Text;
            SiteSettings.CompanyFax = tbxCompanyFax.Text;
            SiteSettings.CustomerPrefix = tbxCustomerPrefix.Text;
            SiteSettings.OrderPrefix = tbxOderPrefix.Text;
            SiteSettings.AdminEmail = tbxAdminEmail.Text;
            SiteSettings.MinimumOrderAmount =Convert.ToDouble(tbxMinimumOrderAmount.Text.ToString());
            SiteSettings.GroundShippingAmount = Convert.ToDouble(tbxFreeGroundShipping.Text.ToString());
            SiteSettings.IsGroundShippingFree = chkIsfreeGround.Checked;
            int stockAlertLevel;
            int.TryParse(tbxStockAlertLevel.Text, out stockAlertLevel);
            SiteSettings.StockAlertLevel = stockAlertLevel;
            SiteSettings.Save();
            uccResponseMessage.SetSuccessMessage(AutoGearCommonMessage.SaveSucceed);
            uccResponseMessage.Visible = true;
        }
        catch (Exception ex)
        {
            uccResponseMessage.SetFailMessage(AutoGearCommonMessage.SaveProblem);
            uccResponseMessage.Visible = true;
        }
    }

    //protected void Delete_Items(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        ReadWriteCustomCollection<Product> products = new ReadWriteCustomCollection<Product>();
    //        foreach (GridViewRow row in grvProduct.Rows)
    //        {
    //            if (row.RowType == DataControlRowType.DataRow)
    //            {
    //                CheckBox cbxSelect = (CheckBox)row.FindControl("cbxSelect");
    //                if (cbxSelect != null && cbxSelect.Checked)
    //                {
    //                    HiddenField hdfProductId = (HiddenField)row.FindControl("hdfProductId");
    //                    products.Add(Product.Load(Convert.ToInt32(hdfProductId.Value)));
    //                }
    //            }
    //        }
    //        Product.RemoveFavoriteProducts(products);
    //        LoadProductDropDownList();
    //        BindFavoriteGrid();
    //    }
    //    catch (Exception ex)
    //    {
    //        uccResponseMessage.SetFailMessage("There was a problem in removing the favorite items, please try again");
    //    }
    //}

    //protected void Add_Items(object sender, EventArgs e)
    //{
    //    Product product = Product.Load(int.Parse(ddlProductList.SelectedValue));
    //    Product.AddToFeturedItem(product);
    //    LoadProductDropDownList();
    //    BindFavoriteGrid();
    //}
}
